package com.cskaoyan.service;

import com.cskaoyan.bean.BO.AdminAdminCreateBO;
import com.cskaoyan.bean.PO.MarketAdmin;
import com.cskaoyan.bean.PO.MarketAdminExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketAdminMapper;
import com.cskaoyan.mapper.MarketCouponUserMapper;
import com.cskaoyan.utils.MyGetCurrentTime;
import com.cskaoyan.utils.MyPageHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName AdminAdminServiceImpl
 * @Description
 * @Author Lxz
 * @Date 2022-8-12 21:25
 */
@Service
public class AdminAdminServiceImpl implements AdminAdminService {

    @Autowired
    MarketAdminMapper marketAdminMapper;


    @Override
    public CommonData<MarketAdmin> list(String username, BasePageInfo info) {
        MyPageHelper.startPage(info);

        MarketAdminExample example = new MarketAdminExample();
        if (info.getOrder() != null && info.getSort() != null) {
            example.setOrderByClause(info.getSort() + " " + info.getOrder());
        }
        MarketAdminExample.Criteria criteria = example.createCriteria();
        if (username != null && !"".equals(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        criteria.andDeletedEqualTo(false);
        List<MarketAdmin> marketAdmins = marketAdminMapper.selectByExample(example);

        PageInfo<MarketAdmin> pageInfo = new PageInfo<>(marketAdmins);
        CommonData data = CommonData.data(pageInfo);

        return data;
    }

    @Override
    public MarketAdmin create(MarketAdmin marketAdmin) {
        Date currentTime = MyGetCurrentTime.getCurrentTime();
        marketAdmin.setAddTime(currentTime);
        marketAdmin.setUpdateTime(currentTime);
        marketAdminMapper.insertSelective(marketAdmin);
        return marketAdmin;
    }

    @Override
    public List<MarketAdmin> judgeRepeat(MarketAdmin marketAdmin) {
        MarketAdminExample example = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(marketAdmin.getUsername());
        criteria.andDeletedEqualTo(false);
        List<MarketAdmin> marketAdmins = marketAdminMapper.selectByExample(example);
        return marketAdmins;
    }


    @Override
    public MarketAdmin update(MarketAdmin marketAdmin) {
        Date currentTime = MyGetCurrentTime.getCurrentTime();
        marketAdmin.setUpdateTime(currentTime);
        marketAdminMapper.updateByPrimaryKeySelective(marketAdmin);
        return marketAdmin;
    }

    @Override
    public void delete(MarketAdmin marketAdmin) {
        Date currentTime = MyGetCurrentTime.getCurrentTime();
        marketAdmin.setUpdateTime(currentTime);
        marketAdmin.setDeleted(true);
        marketAdminMapper.updateByPrimaryKey(marketAdmin);
    }
}
